package presentacion.vista;

import accesoDatos.dao.DaoPedido;
import accesoDatos.util.Log;
import accesoDatos.vo.Empleado;
import accesoDatos.vo.Pedido;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import presentacion.util.Ventana;

public class VistaCajero extends javax.swing.JFrame implements Printable {

    private JFrame parent = null;
    private Empleado empleado;
    private DialogoCambiarContraseña cambiarContraseña = null;
    private List<JButton> botones = new ArrayList<JButton>();
    private List<Pedido> pedidos = new ArrayList<Pedido>();
    private int idPedidoActual = 0;

    /** Creates new form VistaCajero */
    public VistaCajero(JFrame parent, Empleado empleado) {
        initComponents();
        this.parent = parent;
        this.empleado = empleado;
        campoNombre.setText(empleado.getNombre());
        campoCargo.setText(empleado.getCargo());
        campoId.setText(empleado.getId().toString());
        pedidos = DaoPedido.readAll();
        cargarMesas(panelMesas);
        Log.setEvento("Servidor", "INFO", "Se Ejecuta Interfaz de Cajero.");
        Ventana.pantallaCompleta(this);
        this.setVisible(true);
        parent.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void obtenerPedidoMesa(java.awt.event.ActionEvent evt) {
        JButton boton = (JButton) evt.getSource();
        int numMesa = Integer.parseInt(boton.getName());
        double total = 0;
        javax.swing.table.DefaultTableModel modeloTabla = new javax.swing.table.DefaultTableModel();
        modeloTabla.addColumn("Cantidad");
        modeloTabla.addColumn("Producto");
        modeloTabla.addColumn("Precio");

        for (int indexMesas = 0; indexMesas < pedidos.size(); indexMesas++) {
            if (pedidos.get(indexMesas).getMesa() == numMesa) {
                idPedidoActual = pedidos.get(indexMesas).getId();
                for (int i = 0; i < pedidos.get(indexMesas).getProductos().size(); i++) {
                    modeloTabla.addRow(new String[]{"1", pedidos.get(indexMesas).getProductos().get(i).getNombre(), String.valueOf(pedidos.get(indexMesas).getProductos().get(i).getPrecio())});
                    total += pedidos.get(indexMesas).getProductos().get(i).getPrecio();
                }
            }

        }
        tablaPedido.setModel(modeloTabla);
        campoTotal.setText(String.valueOf(total));
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tablaFactura = new javax.swing.JScrollPane();
        tablaPedido = new javax.swing.JTable();
        panelAccionesPedido = new javax.swing.JPanel();
        total = new javax.swing.JLabel();
        campoTotal = new javax.swing.JTextField();
        botonCerrarPedido = new javax.swing.JButton();
        botonImprimiFactura = new javax.swing.JButton();
        panelAccionesEmpleado = new javax.swing.JPanel();
        botonCambiarContraseña = new javax.swing.JButton();
        botonCerrarSesion = new javax.swing.JButton();
        panelDatosEmpleado = new javax.swing.JPanel();
        nombre = new javax.swing.JLabel();
        campoNombre = new javax.swing.JTextField();
        cargo = new javax.swing.JLabel();
        campoCargo = new javax.swing.JTextField();
        id = new javax.swing.JLabel();
        campoId = new javax.swing.JTextField();
        mesas = new javax.swing.JScrollPane();
        panelMesas = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Interfaz Cajero");
        setResizable(false);
        setUndecorated(true);

        tablaPedido.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Cantidad", "Producto", "Precio"
            }
        ));
        tablaFactura.setViewportView(tablaPedido);

        total.setFont(new java.awt.Font("Tahoma", 1, 18));
        total.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        total.setText("TOTAL");

        campoTotal.setEditable(false);

        botonCerrarPedido.setFont(new java.awt.Font("Tahoma", 0, 18));
        botonCerrarPedido.setText("Cerrar Pedido");
        botonCerrarPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCerrarPedidoActionPerformed(evt);
            }
        });

        botonImprimiFactura.setFont(new java.awt.Font("Tahoma", 0, 18));
        botonImprimiFactura.setText("Imprimir Factura");
        botonImprimiFactura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonImprimiFacturaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelAccionesPedidoLayout = new javax.swing.GroupLayout(panelAccionesPedido);
        panelAccionesPedido.setLayout(panelAccionesPedidoLayout);
        panelAccionesPedidoLayout.setHorizontalGroup(
            panelAccionesPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelAccionesPedidoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(botonCerrarPedido, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonImprimiFactura, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
                .addComponent(total, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(54, 54, 54)
                .addComponent(campoTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26))
        );
        panelAccionesPedidoLayout.setVerticalGroup(
            panelAccionesPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelAccionesPedidoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelAccionesPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botonImprimiFactura, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                    .addComponent(botonCerrarPedido, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelAccionesPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(total, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                        .addComponent(campoTotal, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)))
                .addContainerGap())
        );

        botonCambiarContraseña.setFont(new java.awt.Font("Tahoma", 0, 18));
        botonCambiarContraseña.setText("Cambiar Contraseña"); // NOI18N
        botonCambiarContraseña.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCambiarContraseñaActionPerformed(evt);
            }
        });

        botonCerrarSesion.setFont(new java.awt.Font("Tahoma", 0, 18));
        botonCerrarSesion.setText("Cerrar Sesion"); // NOI18N
        botonCerrarSesion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCerrarSesionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelAccionesEmpleadoLayout = new javax.swing.GroupLayout(panelAccionesEmpleado);
        panelAccionesEmpleado.setLayout(panelAccionesEmpleadoLayout);
        panelAccionesEmpleadoLayout.setHorizontalGroup(
            panelAccionesEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelAccionesEmpleadoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(botonCambiarContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addComponent(botonCerrarSesion, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );
        panelAccionesEmpleadoLayout.setVerticalGroup(
            panelAccionesEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelAccionesEmpleadoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelAccionesEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCambiarContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonCerrarSesion, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        panelDatosEmpleado.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos Empleado", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 2, 12), new java.awt.Color(102, 102, 102)));

        nombre.setText("Nombre : ");

        campoNombre.setEditable(false);
        campoNombre.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        cargo.setText("Cargo : ");

        campoCargo.setEditable(false);
        campoCargo.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        id.setText("ID : ");

        campoId.setEditable(false);
        campoId.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        javax.swing.GroupLayout panelDatosEmpleadoLayout = new javax.swing.GroupLayout(panelDatosEmpleado);
        panelDatosEmpleado.setLayout(panelDatosEmpleadoLayout);
        panelDatosEmpleadoLayout.setHorizontalGroup(
            panelDatosEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDatosEmpleadoLayout.createSequentialGroup()
                .addGap(56, 56, 56)
                .addComponent(nombre, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 388, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(133, 133, 133)
                .addComponent(cargo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoCargo, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(139, 139, 139)
                .addComponent(id, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(campoId, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                .addGap(98, 98, 98))
        );
        panelDatosEmpleadoLayout.setVerticalGroup(
            panelDatosEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDatosEmpleadoLayout.createSequentialGroup()
                .addGroup(panelDatosEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(id, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(campoId)
                    .addComponent(nombre, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(campoNombre)
                    .addComponent(cargo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(campoCargo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        mesas.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        mesas.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        mesas.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        javax.swing.GroupLayout panelMesasLayout = new javax.swing.GroupLayout(panelMesas);
        panelMesas.setLayout(panelMesasLayout);
        panelMesasLayout.setHorizontalGroup(
            panelMesasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1218, Short.MAX_VALUE)
        );
        panelMesasLayout.setVerticalGroup(
            panelMesasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1076, Short.MAX_VALUE)
        );

        mesas.setViewportView(panelMesas);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(panelAccionesPedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tablaFactura, javax.swing.GroupLayout.PREFERRED_SIZE, 928, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(panelAccionesEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(43, 43, 43)
                        .addComponent(mesas, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(panelDatosEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(panelAccionesEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(43, 43, 43)
                        .addComponent(tablaFactura, javax.swing.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(panelAccionesPedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(mesas, javax.swing.GroupLayout.DEFAULT_SIZE, 654, Short.MAX_VALUE)))
                .addGap(0, 0, 0)
                .addComponent(panelDatosEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1272)/2, (screenSize.height-738)/2, 1272, 738);
    }// </editor-fold>//GEN-END:initComponents
    private void cargarMesas(JPanel panel) {
        pack();
        int anchoDelPadre = panel.getParent().getWidth();
        panel.setPreferredSize(new Dimension(anchoDelPadre, 1));
        botones.removeAll(botones);
        panel.removeAll();

        int espacioEntreBotones = 10;
        int filas = 0;


        for (int i = 0; i < pedidos.size(); i++) {
            if (pedidos.get(i).getEnCurso() == true) {
                JButton botonMesa = new JButton();
                // Propiedades del boton


                botonMesa.setSize(210, 80);
                botonMesa.setLocation(10, filas + espacioEntreBotones);
                botonMesa.setFont(new Font("Tahoma", 0, 18));
                botonMesa.setName(String.valueOf(pedidos.get(i).getMesa()));
                botonMesa.setText("Mesa " + String.valueOf(pedidos.get(i).getMesa()));
                botonMesa.setToolTipText("Mesa " + String.valueOf(pedidos.get(i).getMesa()));
                botonMesa.addActionListener(new java.awt.event.ActionListener() {

                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        obtenerPedidoMesa(evt);
                    }
                });


                // Ubicacion del proximo boton            
                filas += 90;

                // Adicion del boton
                botones.add(botonMesa);
                panel.add(botones.get(botones.size() - 1));

                //Ampliacion del panel contenedor
                pack();
                panel.setPreferredSize(new Dimension(anchoDelPadre, (int) (botonMesa.getLocation().getY() + 80 + espacioEntreBotones)));
                panel.updateUI();
            }

            // Refresco
            pack();
            panel.updateUI();
        }

    }

    private void botonCambiarContraseñaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCambiarContraseñaActionPerformed
        if (cambiarContraseña == null) {
            cambiarContraseña = new DialogoCambiarContraseña(this, true);
        }

        cambiarContraseña.resetAll();
        cambiarContraseña.setEmpleado(this.empleado);
        cambiarContraseña.pack();
        cambiarContraseña.setVisible(true);
    }//GEN-LAST:event_botonCambiarContraseñaActionPerformed

    private void botonImprimiFacturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonImprimiFacturaActionPerformed
        PrinterJob pj = PrinterJob.getPrinterJob();
        pj.setPrintable(VistaCajero.this);
        pj.printDialog();
        try {
            pj.print();
        } catch (Exception PrintException) {
        }
}//GEN-LAST:event_botonImprimiFacturaActionPerformed

    private void botonCerrarSesionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCerrarSesionActionPerformed
        parent.setVisible(true);
        this.dispose();
        Log.setEvento("Servidor", "INFO", "Se Cierra Interfaz de Cajero.");
        Log.setEvento("Servidor", "INFO", "Se Ejecuta Interfaz de Iniciar Sesion.");
}//GEN-LAST:event_botonCerrarSesionActionPerformed

    private void botonCerrarPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCerrarPedidoActionPerformed
        // TODO add your handling code here:
        Pedido p = DaoPedido.read(idPedidoActual);
        p.setEnCurso(false);
        DaoPedido.update(p);
        cargarMesas(panelMesas);
        javax.swing.table.DefaultTableModel modeloTabla = new javax.swing.table.DefaultTableModel();
        modeloTabla.addColumn("Cantidad");
        modeloTabla.addColumn("Producto");
        modeloTabla.addColumn("Precio");
        tablaPedido.setModel(modeloTabla);
        campoTotal.setText("");
    }//GEN-LAST:event_botonCerrarPedidoActionPerformed

    public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {

        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.black);
        Font fuente = new Font("Tahoma", Font.PLAIN, 12);
        int fontHeight = g2.getFontMetrics().getHeight();
        int fontDesent = g2.getFontMetrics().getDescent();
        double pageHeight = pageFormat.getImageableHeight() - fontHeight;
        double pageWidth = pageFormat.getImageableWidth();
        double tableWidth = (double) tablaPedido.getColumnModel().getTotalColumnWidth();
        double scale = 1;

        Date fechaActual = new Date();
        SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
        String cadenaFecha = formato.format(fechaActual);

        String total = campoTotal.getText();

        String empresa = "TU LUNCH. Para comer bien su casa, luego la nuestra";
        String datos = "Carrera 15 No 77 - 05 • Local 1-07 • Telefono.: 6221554";


        if (tableWidth >= pageWidth) {
            scale = (pageWidth / tableWidth) - 0.1;
        }

        double headerHeightOnPage = tablaPedido.getTableHeader().getHeight() * scale;
        double tableWidthOnPage = (tableWidth * scale);

        double oneRowHeight = (tablaPedido.getRowHeight() + tablaPedido.getRowMargin()) * scale;
        int numRowsOnAPage = (int) ((pageHeight - headerHeightOnPage) / oneRowHeight);
        double pageHeightForTable = oneRowHeight * numRowsOnAPage;
        int totalNumPages = (int) Math.ceil(((double) tablaPedido.getRowCount()) / numRowsOnAPage);
        if (pageIndex >= totalNumPages) {
            return NO_SUCH_PAGE;
        }

        g2.setFont(fuente);
        g2.drawString(empresa, (int) pageWidth / 2 - 45, 35);
        g2.drawString(datos, (int) pageWidth / 2 - 53, 55);
        g2.drawString("Fecha: " + cadenaFecha, 40, 90);
        g2.drawString("Total:               " + total, 400, 110);
        g2.translate(40, 130);

        g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        g2.translate(0f, headerHeightOnPage);
        g2.translate(0f, (-pageIndex * pageHeightForTable));


        if (pageIndex + 1 == totalNumPages) {
            int lastRowPrinted = numRowsOnAPage * pageIndex;
            int numRowsLeft = tablaPedido.getRowCount() - lastRowPrinted;
            g2.setClip(0, (int) (pageHeightForTable * pageIndex), (int) Math.ceil(tableWidthOnPage), (int) Math.ceil((oneRowHeight * numRowsLeft)));

        } else {
            g2.setClip(0, (int) (pageHeightForTable * pageIndex), (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(pageHeightForTable));
        }

        g2.scale(scale, scale);
        tablaPedido.paint(g2);
        g2.scale(1 / scale, 1 / scale);
        g2.translate(0f, (pageIndex * pageHeightForTable));
        g2.translate(0f, -headerHeightOnPage);
        g2.setClip(0, 0, (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(headerHeightOnPage));
        g2.scale(scale, scale);
        tablaPedido.getTableHeader().paint(g2);



        return Printable.PAGE_EXISTS;
    }

    /**
     * @param args the command line arguments
     */
    public static void ejecutar(final JFrame parent,
            final Empleado empleado) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new VistaCajero(parent, empleado);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCambiarContraseña;
    private javax.swing.JButton botonCerrarPedido;
    private javax.swing.JButton botonCerrarSesion;
    private javax.swing.JButton botonImprimiFactura;
    private javax.swing.JTextField campoCargo;
    private javax.swing.JTextField campoId;
    private javax.swing.JTextField campoNombre;
    private javax.swing.JTextField campoTotal;
    private javax.swing.JLabel cargo;
    private javax.swing.JLabel id;
    private javax.swing.JScrollPane mesas;
    private javax.swing.JLabel nombre;
    private javax.swing.JPanel panelAccionesEmpleado;
    private javax.swing.JPanel panelAccionesPedido;
    private javax.swing.JPanel panelDatosEmpleado;
    private javax.swing.JPanel panelMesas;
    private javax.swing.JScrollPane tablaFactura;
    private javax.swing.JTable tablaPedido;
    private javax.swing.JLabel total;
    // End of variables declaration//GEN-END:variables
}
